package com.pure.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pure.common.ReturnData;
import com.pure.common.SysCodeMsg;
import com.pure.common.Util;
import com.pure.db.User;
import com.pure.service.UserService;

@Controller
public class UserController extends BaseController {

	@Resource
	private UserService userService;

	@RequestMapping(value = "login", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public ReturnData login(String username, String password,Integer type) {
		ReturnData returnData = new ReturnData();
		try {
			User user = userService.login(username, password, type);
			if (user != null) {
				returnData.setData(user);
			} else {
				returnData.setCode(SysCodeMsg.CODE_10004);
				returnData.setMessage(SysCodeMsg.MSG_10004);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			returnData.setCode(SysCodeMsg.CODE_10000);
			returnData.setMessage(SysCodeMsg.MSG_10000);
		}
		return returnData;
	}

	@RequestMapping(value = "logout", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public ReturnData logout(HttpServletRequest request) {
		ReturnData returnData = new ReturnData();
		request.getSession().removeAttribute("user");
		return returnData;

	}
	@RequestMapping(value = "register", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public ReturnData register(User user) {
		ReturnData returnData = new ReturnData();
		try {
			if (user == null) {
				returnData.setCode(SysCodeMsg.CODE_10007);
				returnData.setMessage(SysCodeMsg.MSG_10007);
			}
			Integer count = userService.register(user);
			returnData.setData(count);
			return returnData;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			returnData.setCode(SysCodeMsg.CODE_10000);
			returnData.setMessage(SysCodeMsg.MSG_10000);
		}
		return returnData;
	}
	@RequestMapping(value = "updateUser", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public ReturnData updateUser(User user) {
		ReturnData returnData = new ReturnData();
		try {
			if (user == null || user.getId() == null) {
				returnData.setCode(SysCodeMsg.CODE_10007);
				returnData.setMessage(SysCodeMsg.MSG_10007);
			}
			Integer count = userService.updateUser(user);
			returnData.setData(count);
			return returnData;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			returnData.setCode(SysCodeMsg.CODE_10000);
			returnData.setMessage(SysCodeMsg.MSG_10000);
		}
		return returnData;
	}
	@RequestMapping(value = "getUserById", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public ReturnData getUserById(@RequestParam Integer id) {
		ReturnData returnData = new ReturnData();
		try {
			if (id == null) {
				returnData.setCode(SysCodeMsg.CODE_10007);
				returnData.setMessage(SysCodeMsg.MSG_10007);
			}
			User user = userService.getUserById(id);
			returnData.setData(user);
			return returnData;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			returnData.setCode(SysCodeMsg.CODE_10000);
			returnData.setMessage(SysCodeMsg.MSG_10000);
		}
		return returnData;
	}
}
